/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * This is a basic figure to represent a rectangle with a specific keyword (e.g. figures for datatypes or primitive types)
 * * It contains: 1 tag label + 1 icone label + 1 stereotype label + 1 qualified name label + 1 name label
 */
public class TaggedElementFigure extends NodeFigure {

	/**
	 * The content pane.
	 */
	private Figure contentPane;

	/**
	 * 
	 */
	private Label tagLabel;

	/**
	 * constructor to create a TaggedElementFigure.
	 * 
	 * @param keyword 
	 */
	public TaggedElementFigure(String keyword) {
		super();
		this.createTagLabel(keyword);
		ToolbarLayout layout = new ToolbarLayout();
		layout.setStretchMinorAxis(true);
		this.setLayoutManager(layout);

		this.setOpaque(true); // non-transparent figure
		this.setBorder( new LineBorder(PapyrusPlugin.colorManager.get(new RGB(255,255,255))));
		this.setBackgroundColor(PapyrusPlugin.colorManager.get(new RGB(0,0,0)));

		// The area accepting inner figures.
		this.createContentPane(keyword);
	}

	/**
	 * returns a container to add some graphical element as label for instance.
	 * 
	 * @return a container to add some new graphical elements
	 */
	public IFigure getContentPane() {
		return this.contentPane;	
	}

	/**
	 * this method is used to create the content figure.
	 * 
	 * @param keyword 
	 */
	protected void createContentPane(String keyword) {
		this.contentPane = new Figure();
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		this.contentPane.setLayoutManager(layout);
		//this.contentPane.add(new org.eclipse.draw2d.Label(PapyrusPlugin.ST_LEFT+keyword+PapyrusPlugin.ST_RIGHT));
		this.add(this.contentPane);		
	}

	/**
	 * This method is used to create the tag label in the figure.
	 * 
	 * @param keyword 
	 */
	protected void createTagLabel(String keyword) {
		FontData[] fontdata={new FontData(this.fontString, this.fontSize, SWT.NORMAL)};
		Font font = PapyrusPlugin.fontManager.get(fontdata);
		Label label = new Label();
		label.setFont(font);
		label.setForegroundColor(this.fontColor);
		label.setOpaque(false);
		label.setText(PapyrusPlugin.ST_LEFT+keyword+PapyrusPlugin.ST_RIGHT);
		// Add the tag label to the figure at the position 0
		this.add(label, 0);
		this.tagLabel = label;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.NodeFigure#getIconLabelPosition()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected int getIconLabelPosition() {
		return 1;
	}

	public Dimension getMinimumDimension() {
		int width=tagLabel.getTextBounds().width+10;
		int height= tagLabel.getTextBounds().height+10;
		Dimension dim= super.getMinimumDimension();
		dim.height+=height;
		if(dim.width<width){
			dim.width=width;
		}	
		return dim;

	}
}